package com.web.service;

import com.web.dao.SysMenuMapper;
import com.web.domain.WebResponse;
import com.web.entity.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @author xukk
 * @date 2017/9/14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysMenuService {


    @Autowired
    private SysMenuMapper sysMenuMapper;

    public WebResponse addSysMenu(SysMenu sysMenu){
        SysMenu parentMenu=null;
        sysMenu.setLevel(0);
        sysMenu.setEnabled(true);
        sysMenu.setHasNext(false);
        if(sysMenu.getIcon()==null) {
            sysMenu.setIcon("");
        }
        if(sysMenu.getSort()==null) {
            sysMenu.setSort(0);
        }
        if(sysMenu.getPid()>0){
            parentMenu=sysMenuMapper.selectByPrimaryKey(sysMenu.getPid());
            if(parentMenu==null) {
                return WebResponse.fail("父菜单无效");
            } else {
                sysMenu.setLevel(parentMenu.getLevel()+1);
            }
        }
        if (sysMenuMapper.insertSelective(sysMenu) > 0) {
            if(parentMenu!=null&&parentMenu.getHasNext()==false){
                parentMenu.setHasNext(true);
                sysMenuMapper.updateByPrimaryKey(parentMenu);
            }
            return WebResponse.ok("");
        }
        else {
            return WebResponse.fail(WebResponse.UF);
        }

    }

    public WebResponse delSysMenu(Long menuId){
        SysMenu sysMenu=sysMenuMapper.selectByPrimaryKey(menuId);
        if(sysMenu==null) {
            return WebResponse.fail("该对象不存在");
        }
        sysMenuMapper.deleteByPrimaryKey(menuId);
        if(sysMenu.getPid()>0){
            List<SysMenu> list=sysMenuMapper.selectByPid(sysMenu.getPid());
            if(list!=null&&list.size()==0){
                sysMenuMapper.updateHasNext(sysMenu.getPid(),false);
            }
        }
        return WebResponse.ok("");
    }
    public WebResponse updateMenu(SysMenu sysMenu){
        if(sysMenu.getId()==null||sysMenu.getId()<=0){
            return WebResponse.fail("ID 非法");
        }
        SysMenu sysMenu1=sysMenuMapper.selectByPrimaryKey(sysMenu.getId());
        if(sysMenu1==null) {
            return WebResponse.fail("对象不存在");
        }
        Long bPid=sysMenu1.getPid();
        if(sysMenu.getPid()!=null&&sysMenu.getPid()>0){
            SysMenu pSys=sysMenuMapper.selectByPrimaryKey(sysMenu.getPid());
            if(pSys==null) {
                return WebResponse.fail("父菜单不存在");
            }
        }
        Boolean flag=false;
        if(!bPid.equals(sysMenu.getPid())){
        sysMenu1.setPid(sysMenu.getPid());
        flag=true;
        }
        sysMenu1.setSort(sysMenu.getSort());
        sysMenu1.setUrl(sysMenu.getUrl());
        sysMenu1.setIcon(sysMenu.getIcon());
        sysMenu1.setText(sysMenu.getText());
        if(sysMenuMapper.updateByPrimaryKey(sysMenu1)>0){
            if(flag){
                List<SysMenu> list=sysMenuMapper.selectByPid(sysMenu.getPid());
                if(list!=null&&list.size()==0){
                    sysMenuMapper.updateHasNext(sysMenu.getPid(),false);
                }else {
                    sysMenuMapper.updateHasNext(sysMenu.getPid(),true);
                }
                List<SysMenu> list1=sysMenuMapper.selectByPid(bPid);
                if(list1!=null&&list1.size()==0){
                    sysMenuMapper.updateHasNext(bPid,false);
                }else {
                    sysMenuMapper.updateHasNext(bPid,true);
                }
            }


            return WebResponse.ok("");
        }

        else {
            return WebResponse.fail("修改失败");
        }
    }

}
